package com.example.war;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.PointF;
import android.opengl.GLUtils;
import android.util.FloatMath;
import android.util.Log;

public class Stars {
    private Context mContext;
    private static final int COUNT = 30;
    private static final int TYPE_NUM = 3;
    private Quad mQuads[] = new Quad[COUNT];
    private Star[] mStars = new Star[COUNT];
    private Point mScrSize = new Point();
    private Point mBmpSize = new Point();
    private int mTexId;
    
    public Stars(Context context, GL10 gl) {
        mContext = context;
        int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);
        mTexId = textures[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexId);
        Bitmap bitmap = Utils.loadRawImage(mContext.getResources(), R.raw.star);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
        mBmpSize.x = bitmap.getWidth();
        mBmpSize.y = bitmap.getHeight();
        bitmap.recycle();

        for (int i = 0; i<TYPE_NUM; i++) {
        	mQuads[i] = new Quad(false);
        	mQuads[i].setSize((int)(mBmpSize.x*(1+i*0.1)), (int)(mBmpSize.y*(1+i*0.1)));
        	mQuads[i].setTexture(gl, mTexId);
        }
        
        for (int i = 0; i<COUNT; i++) {
            mStars[i] = new Star();
        }
        
        mScrSize = Utils.getScreenSize(context);
    }
    
    public void draw(GL10 gl) {
    	//mQuads[1].draw(gl, 0, 100, 150, 0, true);
    	long currentTime = System.currentTimeMillis();
    	Star star;
    	for (int i = 0; i < COUNT; i++) {
			star = mStars[i];
			if (!star.active) {
				initStar(star);
			}
			int t = (int) (currentTime - star.startTime);
			if (t < 50)
				continue;
			t /= 50;
			float dist = star.v * t + star.a * t * t / 2;
			float distX = dist * FloatMath.cos(star.angle);
			float distY = dist * FloatMath.sin(star.angle);
			//Log.d("zzzz","v:"+star.v+" t:"+t+" a:"+star.a+"   distX:"+distX+" distY:"+distY);
			float xPos = star.position.x + distX;
			float yPos = star.position.y + distY;
			if (xPos > mScrSize.x /2 || yPos > mScrSize.y/2) {
				initStar(star);
			}
    		//Log.d("zzzz","Meteor["+i+"]:"+xPos+", "+yPos);
    		star.quad.draw(gl, 0, xPos, yPos, 0, true);
    	}
    }
    
    private void initStar(Star star) {
    	long currentTime = System.currentTimeMillis();
    	Random random = new Random(currentTime);
    	star.startTime = currentTime + 300 + random.nextInt(10) * 100;
    	//Log.d("zzzz","@ scrW:"+mScrSize.x+"  scrH:"+mScrSize.y);
    	
    	star.v = 5 + random.nextInt(5);
    	star.a = 0;
    	star.angle = (float)Math.toRadians(35+random.nextInt(35));
    	
    	// y = ax +b
    	star.position.x =  random.nextInt(mScrSize.x/4);
    	star.position.y = -mScrSize.y/2+random.nextInt(mScrSize.y/2);;

        //Log.d("zzzz","====== Init Pos:"+star.position.x+", "+star.position.y);

   	    star.active = true;
    	star.quad = mQuads[random.nextInt(TYPE_NUM)];
    }
    
    class Star {
        long startTime;
        PointF position = new PointF();
        float v;
        float a;
        float angle;
        boolean active;
        Quad quad;
    }
    
    public void clean (GL10 gl) {
        for (Quad quad: mQuads) {
            quad.deleteTexture(gl);
        }
    }
}
